package com.dk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dk.mapper.CodeRuleMapper;
import com.dk.pojo.CodeRule;
import com.dk.pojo.CodeRuleExample;
import com.dk.pojo.Location;
import com.dk.pojo.User;
import com.dk.result.PageResult;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import com.dk.service.CodeRuleService;
import com.dk.utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CodeRuleServiceImpl implements CodeRuleService {

    @Autowired
    private CodeRuleMapper codeRuleMapper;
    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public PageResult<CodeRule> getCodeRuleInfo(Integer pageNum, Integer pageSize, String ruleName, String ruleCode, String enableFlag) {
        CodeRuleExample example = new CodeRuleExample();
        if(ruleName != null && ruleName.length() > 0){
            example.createCriteria().andRuleNameLike("%"+ruleName+"%");
        }
        if(ruleCode != null && ruleCode.length() > 0){
            example.createCriteria().andRuleCodeEqualTo(ruleCode);
        }
        if(enableFlag != null && enableFlag.length() > 0){
            example.createCriteria().andEnableFlagEqualTo(enableFlag);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<CodeRule> codeRules = codeRuleMapper.selectByExample(example);
        PageInfo<CodeRule> pb = new PageInfo<>(codeRules);
        PageResult<CodeRule> result = new PageResult<>(pb.getPageNum(), pb.getPageSize(), pb.getTotal(),pb.getList());
        return result;
    }

    @Override
    public Result addCodeRule(CodeRule codeRule, String token) {
        try {
            Claims claims = jwtUtil.parseJwt(token);
            String subject = claims.getSubject();
            User user = JSONObject.parseObject(subject, User.class);
            codeRule.setCreateBy(user.getUsername());
            codeRule.setCreateTime(new Date());
            codeRuleMapper.insertSelective(codeRule);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public Result updateCodeRule(CodeRule codeRule, String token) {
        try {
            Claims claims = jwtUtil.parseJwt(token);
            String subject = claims.getSubject();
            User user = JSONObject.parseObject(subject, User.class);
            codeRule.setUpdateBy(user.getUsername());
            codeRule.setUpdateTime(new Date());
            codeRuleMapper.updateByPrimaryKeySelective(codeRule);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public void deleteCodeRule(Long ruleId) {
        codeRuleMapper.deleteByPrimaryKey(ruleId);
    }

    @Override
    public List<CodeRule> selectByRuleCode(String ruleCode) {
        //根据编码查询信息
        CodeRuleExample example = new CodeRuleExample();
        example.createCriteria().andRuleCodeEqualTo(ruleCode);
        return codeRuleMapper.selectByExample(example);
    }
}